{% extends 'base.html' %}

{% block title %}评论管理 - Will's Blog{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-3">
        {% include 'admin/sidebar.html' %}
    </div>
    
    <div class="col-md-9">
        <h1 class="mb-4">评论管理</h1>
        
        <div class="card">
            <div class="card-body">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>评论内容</th>
                            <th>文章</th>
                            <th>评论者</th>
                            <th>时间</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for comment in comments.items %}
                        <tr>
                            <td>{{ comment.body|truncate(50) }}</td>
                            <td>
                                <a href="{{ url_for('main.post', slug=comment.post.slug) }}" target="_blank">
                                    {{ comment.post.title|truncate(30) }}
                                </a>
                            </td>
                            <td>{{ comment.author.username }}</td>
                            <td>{{ comment.created.strftime('%Y-%m-%d') }}</td>
                            <td>
                                {% if comment.approved %}
                                    <span class="badge bg-success">已批准</span>
                                {% else %}
                                    <span class="badge bg-warning text-dark">待审核</span>
                                {% endif %}
                            </td>
                            <td>
                                <div class="btn-group btn-group-sm">
                                    {% if not comment.approved %}
                                    <form method="post" action="{{ url_for('admin.approve_comment', id=comment.id) }}" class="d-inline">
                                        <button type="submit" class="btn btn-success">
                                            <i class="bi bi-check-circle"></i>
                                        </button>
                                    </form>
                                    {% endif %}
                                    <form method="post" action="{{ url_for('admin.delete_comment', id=comment.id) }}" class="d-inline">
                                        <button type="submit" class="btn btn-danger" 
                                                onclick="return confirm('确定要删除这条评论吗？')">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
                
                <!-- 分页 -->
                {% if comments.pages > 1 %}
                    <nav aria-label="Page navigation">
                        <ul class="pagination justify-content-center">
                            {% if comments.has_prev %}
                                <li class="page-item">
                                    <a class="page-link" href="{{ url_for('admin.comments', page=comments.prev_num) }}" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                            {% else %}
                                <li class="page-item disabled">
                                    <a class="page-link" href="#" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                            {% endif %}
                            
                            {% for page in range(1, comments.pages + 1) %}
                                {% if page == comments.page %}
                                    <li class="page-item active">
                                        <a class="page-link" href="{{ url_for('admin.comments', page=page) }}">{{ page }}</a>
                                    </li>
                                {% else %}
                                    <li class="page-item">
                                        <a class="page-link" href="{{ url_for('admin.comments', page=page) }}">{{ page }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}
                            
                            {% if comments.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="{{ url_for('admin.comments', page=comments.next_num) }}" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                            {% else %}
                                <li class="page-item disabled">
                                    <a class="page-link" href="#" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}